package org.jiangwei.cmput696.entitylinking.tacindex;

import java.util.LinkedList;
import java.util.List;

import org.apache.lucene.document.Document;

public class TacDocumentPool {
	
	private List<Document> luceneDocs = new LinkedList<Document>();
	
	private Object syncLock = new Object();
	
	private boolean stopped = false;
	
	private int poppedCount = 0;
	
	public TacDocumentPool() {
		
	}
	
	public boolean isStopped() {
		return stopped && luceneDocs.size() == 0;
	}

	public void setStopped(boolean stopped) {
		synchronized (this.syncLock) {
			this.stopped = stopped;
		}
	}

	public void push(Document doc) {
		synchronized (this.syncLock) {
			luceneDocs.add(doc);
		}
	}
	
	public Document pop() {
		synchronized (this.syncLock) {
			if (luceneDocs.size() == 0) {
				return null;
			}
			Document result = luceneDocs.get(0);
			luceneDocs.remove(0);
			
			if (++poppedCount % 1000 == 0) {
				System.out.println(poppedCount);
			}
			return result;
		}
	}
}
